package org.jeecg.modules.nikeProject.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.nikeProject.entity.*;
import org.jeecg.modules.nikeProject.entity.vo.*;
import org.jeecg.modules.nikeProject.mapper.*;
import org.jeecg.modules.nikeProject.service.IParseService;
import org.jeecg.modules.oss.entity.OssFile;
import org.jeecg.modules.oss.mapper.OssFileMapper;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.jeecg.modules.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.alibaba.fastjson.JSON.parseArray;
import static com.alibaba.fastjson.JSON.parseObject;

@Service
@Slf4j
public class ParseServiceImpl implements IParseService {
    @Autowired
    private SportsProjectMapper sportsProjectMapper;
    @Autowired
    private SportsPersonalInfoMapper sportsPersonalInfoMapper;
    @Autowired
    private SportsPersonalWorkExperienceMapper sportsPersonalWorkExperienceMapper;
    @Autowired
    private SportsPersonalEduMapper sportsPersonalEduMapper;
    @Autowired
    private SportsProjectFileMapper sportsProjectFileMapper;
    @Autowired
    private SportsCaseReportMapper sportsCaseReportMapper;
    @Autowired
    private SportsReferenceMapper sportsReferenceMapper;
    @Autowired
    private SportsQualificationDocMapper sportsQualificationDocMapper;
    @Autowired
    private SportsWorkInfoMapper sportsWorkInfoMapper;
    @Autowired
    private OssFileMapper ossFileMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SportsJudgeMapper sportsJudgeMapper;
    @Autowired
    private SportsGroupMapper sportsGroupMapper;
    @Autowired
    private SportsJudgeGroupMapper sportsJudgeGroupMapper;
    @Autowired
    private SportsWorkInfoSchoolMapper sportsWorkInfoSchoolMapper;
    @Autowired
    private AreaProvinceMapper areaProvinceMapper;
    @Autowired
    private AreaCityMapper areaCityMapper;
    @Autowired
    private AreaCountyMapper areaCountyMapper;


    @Override
    public Result<?> setAreaToRedis(){
        List<AreaObj> provinceObjs = sportsProjectMapper.getAllProvince();
        redisUtil.set(CommonConstant.AREA_PROVINCE, provinceObjs);

        List<AreaObj> countyObjs = sportsProjectMapper.getAllCounty();
        redisUtil.set(CommonConstant.AREA_COUNTY, countyObjs);

        List<AreaObj> cityObjs = sportsProjectMapper.getAllCity();
        redisUtil.set(CommonConstant.AREA_CITY, cityObjs);
        return Result.OK("success");
    }

    @Override
    @Transactional
    public Result<?> parseJudge() {
        log.info("----------------开始解析-------------------");
        String JSON_PATH = "E:\\project\\NIKE项目\\2024-1.json";
        File file = new File(JSON_PATH);
        try {
            String content = FileUtils.readFileToString(file, "UTF-8");
            List<ParseJudgeVO> judgeVOS = parseArray(content, ParseJudgeVO.class);
            List<SportsJudge> sportsJudgeList = new ArrayList<>();
            List<SysUser> userList = new ArrayList<>();
            List<SysUserRole> userRoleList = new ArrayList<>();
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            if (null != judgeVOS){
                judgeVOS.forEach(x ->{
                    SportsJudge sportsJudge = new SportsJudge();
                    sportsJudge.setId(IdWorker.getIdStr());
                    sportsJudge.setProvince(x.getProvince());
                    sportsJudge.setMobile(x.getMobile());
                    sportsJudge.setName(x.getName());
                    sportsJudge.setYear(String.valueOf(year));
                    sportsJudge.setCreateTime(new Date());
                    sportsJudgeList.add(sportsJudge);

                    SysUser sysUser = new SysUser();
                    sysUser.setRealname(x.getName());
                    sysUser.setUsername(x.getMobile());
                    sysUser.setPhone(x.getMobile());
                    sysUser.setProvince(x.getProvince());
                    String userId = IdWorker.getIdStr();
                    sysUser.setId(userId);
                    sysUser.setDelFlag(CommonConstant.DEL_FLAG_0);
                    String salt = oConvertUtils.randomGen(8);
                    sysUser.setSalt(salt);
                    sysUser.setStatus(1);
                    String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), CommonConstant.DEFAULT_PWD, salt);
                    sysUser.setPassword(passwordEncode);
                    userList.add(sysUser);

                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setUserId(userId);
                    sysUserRole.setRoleId("20241226001");
                    userRoleList.add(sysUserRole);
                });
            }
            sportsJudgeMapper.batchInsert(sportsJudgeList);
            sysUserMapper.batchInsert(userList);
            sysUserRoleMapper.batchInsert(userRoleList);
        }catch (IOException e) {
            e.printStackTrace();
            return Result.error("解析文件异常,请联系管理员！");
        }
        log.info("----------------解析完成！！-------------------");
        return Result.OK();
    }

    public SportsJudge getSportsJudge(String phone, String province, String name, Integer year){
        LambdaQueryWrapper<SportsJudge> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsJudge::getMobile, phone)
                .eq(SportsJudge::getName, name)
                .eq(SportsJudge::getProvince, province)
                .eq(SportsJudge::getYear, year)
                .eq(SportsJudge::getIzActive, CommonConstant.COMMON_STATUS_N);
        return sportsJudgeMapper.selectOne(lambdaQueryWrapper);
    }


    @Override
    @Transactional
    public Result<?> parseExcelToJson() {
        log.info("------------解析评委数据入库----------------");
        String JSON_PATH = "E:\\project\\NIKE项目\\excelToJson.json";
        File file = new File(JSON_PATH);
        try {
            String content = FileUtils.readFileToString(file, "UTF-8");
            List<ImportJudgeVO> judgeVOS = parseArray(content, ImportJudgeVO.class);
            List<SportsJudge> judgeList = new ArrayList<>();
            List<SportsGroup> groupList = new ArrayList<>();
            List<SportsJudgeGroup> judgeGroupList = new ArrayList<>();
            List<SysUser> userList = new ArrayList<>();
            List<SysUserRole> userRoleList = new ArrayList<>();
            judgeVOS.forEach(x -> {
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                SportsJudge sportsJudge = getSportsJudge(x.getPhone(), x.getProvince(), x.getName(), year);

                if (null == sportsJudge) {
                    String judgeId = IdWorker.getIdStr();
                    sportsJudge = new SportsJudge();
                    sportsJudge.setId(judgeId);
                    sportsJudge.setName(x.getName());
                    sportsJudge.setProvince(x.getProvince());
                    sportsJudge.setMobile(x.getPhone());
                    sportsJudge.setYear(String.valueOf(year));
                    sportsJudge.setCreateBy("admin");
                    sportsJudge.setCreateTime(new Date());
                    judgeList.add(sportsJudge);
                }

                SportsGroup sportsGroup = new SportsGroup();
                String groupId = IdWorker.getIdStr();
                sportsGroup.setGroupName(x.getGroupName());
                String judgeStage = x.getJudgeStage();
                sportsGroup.setRoleTypeName(judgeStage);
                String[] arr = null;
                if (judgeStage.contains(",")) {
                    arr = judgeStage.split(",");
                } else {
                    arr = new String[]{judgeStage};
                }
                log.info("arrr ====={}", arr.length);
                sportsGroup.setRoleType(getJudgeStage(arr));
                sportsGroup.setCreateBy("admin");
                sportsGroup.setId(groupId);
                groupList.add(sportsGroup);

                SportsJudgeGroup sportsJudgeGroup = new SportsJudgeGroup();
                sportsJudgeGroup.setGroupId(groupId);
                sportsJudgeGroup.setJudgeId(sportsJudge.getId());
                sportsJudgeGroup.setBatchTime(getJudgeTime(x.getJudgeTime()));
                judgeGroupList.add(sportsJudgeGroup);

                SysUser sysUser = sysUserMapper.getUserByName(x.getPhone());
                if (sysUser == null) {
                    sysUser = new SysUser();
                    sysUser.setRealname(x.getName());
                    sysUser.setUsername(x.getPhone());
                    sysUser.setProvince(x.getProvince());
                    sysUser.setPhone(x.getPhone());
                    sysUser.setStatus(1);
                    int gender = "男".equals(x.getSex()) ? 1 : 2;
                    sysUser.setSex(gender);
                    String userId = IdWorker.getIdStr();
                    sysUser.setId(userId);
                    sysUser.setDelFlag(CommonConstant.DEL_FLAG_0);
                    String salt = oConvertUtils.randomGen(8);
                    sysUser.setSalt(salt);
                    String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), CommonConstant.DEFAULT_PWD, salt);
                    sysUser.setPassword(passwordEncode);
                    sysUser.setCreateBy("admin");
                    userList.add(sysUser);

                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setUserId(userId);
                    sysUserRole.setRoleId("20241226001");
                    userRoleList.add(sysUserRole);
                }
            });

            if (null != judgeList && judgeList.size() > 0) {
                sportsJudgeMapper.batchInsert(judgeList);
            }
            if (null != groupList && groupList.size() > 0) {
                sportsGroupMapper.batchInsert(groupList);
            }
            if (null != judgeGroupList && judgeGroupList.size() > 0) {
                sportsJudgeGroupMapper.batchInsert(judgeGroupList);
            }
            if (null != userList && userList.size() > 0) {
                sysUserMapper.batchInsert(userList);
            }
            if (null != userRoleList && userRoleList.size() > 0) {
                sysUserRoleMapper.batchInsert(userRoleList);
            }
            log.info("---------------解析完成！---------------");
            return Result.OK("解析评审入库入库成功！");
        }catch (IOException e) {
            e.printStackTrace();
            return Result.error("解析文件异常,请联系管理员！");
        }
    }

    public List<SportsProjectFile> getProjectFiles(String projectId, String fileType){
        LambdaQueryWrapper<SportsProjectFile> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SportsProjectFile::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsProjectFile::getProjectId, projectId);
        lambdaQueryWrapper.eq(SportsProjectFile::getFileType, fileType);
        return sportsProjectFileMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public Result<?> parseRepresentativeResult(String projectId) {
        List<SportsPersonalInfo> list = getRepresentativeResult(projectId);
        if (null != list && list.size() > 0) {
            list.forEach(x ->{
                List<SportsProjectFile> fileList = new ArrayList<>();
                List<OssFile> ossFiles = new ArrayList<>();
                //判断是否已经写过数据进文件表
                List<SportsProjectFile> projectFiles =  getProjectFiles(x.getProjectId(), CommonConstant.OSS_FILE_TYPE_RESULT);
                if (projectFiles == null || projectFiles.size() == 0) {
                    log.info("projectId={}", x.getProjectId());
                    log.info("representativeResult={}", x.getRepresentativeResult());
                    JSONArray arr = JSONObject.parseArray(x.getRepresentativeResult());
                    for (int i = 0; i < arr.size(); i++) {
                        String ossId = IdWorker.getIdStr();
                        SportsProjectFile sportsProjectFile = new SportsProjectFile();
                        sportsProjectFile.setId(IdWorker.getIdStr());
                        sportsProjectFile.setProjectId(x.getProjectId());
                        sportsProjectFile.setOssFileId(ossId);
                        sportsProjectFile.setCreateBy("admin");
                        sportsProjectFile.setFileType(CommonConstant.OSS_FILE_TYPE_RESULT);
                        fileList.add(sportsProjectFile);

                        OssFile ossFile = new OssFile();
                        ossFile.setId(ossId);
                        String url = arr.get(i).toString();
                        ossFile.setUrl(url.replace("http://www.huolixiaoyuan.com/upload", "https://nike-huolixiaoyuan.oss-rg-china-mainland.aliyuncs.com/rz_uploads"));
                        if (!"".equals(url) && url.length() > 10) {
                            ossFile.setFileName(url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".")));
                        }
                        ossFiles.add(ossFile);
                    }
                    if (null != fileList && fileList.size() > 0) {
                        sportsProjectFileMapper.batchInsert(fileList);
                    }

                    if (null != ossFiles && ossFiles.size() > 0) {
                        ossFileMapper.batchInsert(ossFiles);
                    }
                }
            });
        }

        return Result.OK("解析成功！");
    }

    @Override
    public Result<?> parseShipin() {
        log.info("-----------------------开始解析-------------------------");
        long startTime = System.currentTimeMillis();
        //1.读取JSO数据
        log.info("1.读取JSON文件");
        String JSON_PATH = "D:\\work\\nike\\dashi.json";
        File file = new File(JSON_PATH);
        try {
            String content = FileUtils.readFileToString(file, "UTF-8");
            List<Dashi> selectionApplies = parseArray(content, Dashi.class);
            log.info("解析案例申报书");
            for (Dashi apply : selectionApplies) {
                String shenbaoshu = apply.getShenbaoshu_json();
                String projectId = apply.getId();

                Map<String, Object> map = (Map<String, Object>) JSONObject.parse(shenbaoshu);
                String action = map.get("action").toString();
                Map<String, Object> actionMap = (Map<String, Object>) JSONObject.parse(action);
                actionMap.forEach((key, value) -> {
                    String actionName = actionMap.get(key).toString();
                    JSONArray jsonArray = JSONObject.parseArray(actionName);
                    for (int j = 0; j < jsonArray.size(); j++) {
                        SportsWorkInfoSchool sportsWorkInfoSchool = new SportsWorkInfoSchool();
                        sportsWorkInfoSchool.setId(IdWorker.getIdStr());
                        sportsWorkInfoSchool.setProjectId(projectId);
                        JSONObject obj = JSONObject.parseObject(jsonArray.get(j).toString());
                        Object objName = obj.get("name");
                        if (null != objName) {
                            String name = obj.get("name").toString();
                            sportsWorkInfoSchool.setActivityName(name);
                        }
                        Object infoObj = obj.get("info");
                        if (null != infoObj) {
                            String info = obj.get("info").toString();
                            sportsWorkInfoSchool.setActivityMsg(info);
                        }
                        sportsWorkInfoSchool.setCreateBy("admin");
                        sportsWorkInfoSchool.setIzActive(CommonConstant.COMMON_STATUS_N);
                        sportsWorkInfoSchool.setCreateTime(new Date());
                        sportsWorkInfoSchool.setPromotionType("1");
                        String url = obj.get("shipin").toString();
                        if (null != url && !"".equals(url)) {
                            String ossFileId = IdWorker.getIdStr();
                            SportsProjectFile sportsProjectFile = new SportsProjectFile();
                            sportsProjectFile.setId(IdWorker.getIdStr());
                            sportsProjectFile.setProjectId(projectId);
                            sportsProjectFile.setOssFileId(ossFileId);
                            sportsProjectFile.setIzActive(CommonConstant.COMMON_STATUS_N);
                            sportsProjectFile.setFileType(CommonConstant.OSS_FILE_TYPE_VIDEO);
                            sportsProjectFile.setCreateBy("admin");
                            sportsProjectFile.setCreateTime(new Date());
                            sportsProjectFileMapper.insert(sportsProjectFile);

                            OssFile ossFile = new OssFile();
                            ossFile.setId(ossFileId);
                            ossFile.setFileName(url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".")));
                            ossFile.setUrl(url);
                            ossFile.setCreateBy("admin");
                            ossFile.setCreateTime(new Date());
                            ossFileMapper.insert(ossFile);
                        }
                        sportsWorkInfoSchoolMapper.insert(sportsWorkInfoSchool);
                    }
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.OK("解析完成");
    }

    @Override
    public Result<?> parseArea() throws Exception {
        String JSON_PATH = "D:\\work\\area.json";
        File file = new File(JSON_PATH);
        String content = FileUtils.readFileToString(file, "UTF-8");
        List<AreaProvince> provinceList = new ArrayList<>();
        List<AreaCity> cityList = new ArrayList<>();
        List<AreaCounty> countyList = new ArrayList<>();

        JSONArray jsonArray = JSONObject.parseArray(content);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject provinceObj = JSONObject.parseObject(jsonArray.get(i).toString());
            String provinceName = provinceObj.get("name").toString();
            String provinceCode = provinceObj.get("code").toString();
            AreaProvince areaProvince = new AreaProvince();
            areaProvince.setProvinceCode(provinceCode);
            areaProvince.setName(provinceName);
            areaProvince.setId(IdWorker.getIdStr());
            provinceList.add(areaProvince);
            JSONArray cityArr = provinceObj.getJSONArray("city");
            if (cityArr != null && cityArr.size() > 0) {
                for (int j = 0; j < cityArr.size(); j++) {
                    JSONObject cityObj = JSONObject.parseObject(cityArr.get(j).toString());
                    String cityName = cityObj.get("name").toString();
                    String cityCode = cityObj.get("code").toString();
                    AreaCity areaCity = new AreaCity();
                    areaCity.setCityCode(cityCode);
                    areaCity.setName(cityName);
                    areaCity.setProvinceCode(provinceCode);
                    areaCity.setId(IdWorker.getIdStr());
                    cityList.add(areaCity);
                    JSONArray areaArr = cityObj.getJSONArray("area");
                    for (int k= 0; k < areaArr.size(); k++) {
                        JSONObject areaObj = JSONObject.parseObject(areaArr.get(k).toString());
                        String areaName = areaObj.get("name").toString();
                        String areaCode = areaObj.get("code").toString();
                        AreaCounty areaCounty = new AreaCounty();
                        areaCounty.setCountyCode(areaCode);
                        areaCounty.setName(areaName);
                        areaCounty.setCityCode(cityCode);
                        areaCounty.setId(IdWorker.getIdStr());
                        countyList.add(areaCounty);
                    }
                }
            }
        }

        if (provinceList.size() > 0) {
            areaProvinceMapper.batchInsert(provinceList);
            areaCityMapper.batchInsert(cityList);
            areaCountyMapper.batchInsert(countyList);
        }

        return Result.OK("success");
    }

    public List<SportsPersonalInfo> getRepresentativeResult(String projectId){
        LambdaQueryWrapper<SportsPersonalInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsPersonalInfo::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.likeRight(SportsPersonalInfo::getProjectId, projectId);
        lambdaQueryWrapper.likeRight(SportsPersonalInfo::getRepresentativeResult, "[\"http");
        return sportsPersonalInfoMapper.selectList(lambdaQueryWrapper);
    }

    public int getJudgeTime(String judgeTime){
        int batchTime = 0;
        if (CommonConstant.FISRT_TIME_NAME.equals(judgeTime)){
            batchTime = CommonConstant.FISRT_TIME;
        }
        if (CommonConstant.SECOND_TIME_NAME.equals(judgeTime)){
            batchTime = CommonConstant.SECOND_TIME;
        }
        if (CommonConstant.DISPUTE_FISRT_TIME_NAME.equals(judgeTime)){
            batchTime = CommonConstant.DISPUTE_FISRT_TIME;
        }
        if (CommonConstant.DISPUTE_SECOND_TIME_NAME.equals(judgeTime)){
            batchTime = CommonConstant.DISPUTE_SECOND_TIME;
        }
        return batchTime;
    }

    public String getJudgeStage(String [] arr){
        List<String> roleType = new ArrayList<>();
        for (int i = 0; i < arr.length; i ++){
            if (CommonConstant.ROLE_TYPE_TE_NAME.equals(arr[i])){
                roleType.add(CommonConstant.ROLE_TYPE_TE);
            }
            if (CommonConstant.ROLE_TYPE_SM_NAME.equals(arr[i])){
                roleType.add(CommonConstant.ROLE_TYPE_SM);
            }
            if (CommonConstant.ROLE_TYPE_SR_NAME.equals(arr[i])){
                roleType.add(CommonConstant.ROLE_TYPE_SR);
            }
            if (CommonConstant.ROLE_TYPE_PA_NAME.equals(arr[i])){
                roleType.add(CommonConstant.ROLE_TYPE_PA);
            }
        }
        return roleType.stream().collect(Collectors.joining(","));
    }

    /**
     * 读取json文件后，调用线程进行解析写入相关表
     * @return
     */
    @Override
    public Result<?> parseApplyData() {
        log.info("-----------------------开始解析-------------------------");
        long startTime = System.currentTimeMillis();
        //1.读取JSO数据
        log.info("1.读取JSON文件");
        String JSON_PATH = "D:\\work\\nike\\selection_apply_202504051857.json";
        File file = new File(JSON_PATH);
        try {
            String content = FileUtils.readFileToString(file, "UTF-8");
            List<SelectionApply> selectionApplies = parseArray(content, SelectionApply.class);
            log.info("==================开始进入线程分配案例==============");
            ExecutorService executor = Executors.newFixedThreadPool(20);
            for (SelectionApply apply : selectionApplies) {
                executor.submit(() -> {
                    try {
                        parse(apply);
//                        updateSchoolStage(apply);
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                });
            }
            executor.shutdown();
            executor.awaitTermination(5, TimeUnit.HOURS); // 等待所有任务完成，或者根据需要调整等待时间
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            return Result.error("解析文件异常,请联系管理员！");
        }
        long endTime = System.currentTimeMillis();
        log.info("批量解析数据时长==={}", endTime-startTime);
        return Result.OK();
    }

    @Override
    public Result<?> parseHisTopData() {
        log.info("-----------------------开始解析-------------------------");
        long startTime = System.currentTimeMillis();
        //1.读取JSO数据
        log.info("1.读取JSON文件");
        String JSON_PATH = "E:\\project\\NIKE项目\\2017-2020.json";
        File file = new File(JSON_PATH);
        try {
            String content = FileUtils.readFileToString(file, "UTF-8");
            List<SelectionApply> selectionApplies = parseArray(content, SelectionApply.class);
            ExecutorService executor = Executors.newFixedThreadPool(20);
            for (SelectionApply apply : selectionApplies) {
                executor.submit(() -> {
                    try {
                        String projectId = "NIKE" + apply.getYear() + apply.getId();
                        SportsProject sportsProject = new SportsProject();
                        SportsPersonalInfo sportsPersonalInfo = new SportsPersonalInfo();
                        SportsCaseReport sportsCaseReport = new SportsCaseReport();
                        sportsProject.setId(projectId);
                        sportsProject.setYear(apply.getYear());
                        sportsProject.setPrize(apply.getPrize());
                        sportsProject.setCaseType(getCaseType(apply.getType()));
                        sportsProject.setProjectName(apply.getName());
                        sportsProject.setRoleType(getRoleType(apply.getIdentity()));
                        String contentJson = apply.getContent_json();
                        Map<String, Object> map = (Map<String, Object>) JSONObject.parse(contentJson);
                        if (null != map.get("school") && !"".equals(map.get("school").toString())) {
                            String schoolName = map.get("school").toString();
                            sportsProject.setSchoolName(schoolName);
                            sportsPersonalInfo.setSchoolName(schoolName);
                        }

                        String shenbaorenJson = apply.getShenbaoren_json();
                        Map<String, Object> shenbaorenMap = (Map<String, Object>) JSONObject.parse(shenbaorenJson);
                        sportsPersonalInfo.setSchoolPeriod(apply.getXueduan());
                        sportsProject.setUserName(shenbaorenMap.get("name").toString());
                        String mobile = "";
                        if (null != shenbaorenMap.get("mobile") && !"".equals(shenbaorenMap.get("mobile").toString())) {
                            mobile = shenbaorenMap.get("mobile").toString();
                            sportsPersonalInfo.setPhone(mobile);
                        }
                        sportsPersonalInfo.setIdNumber(shenbaorenMap.get("shenfenzheng").toString());
                        sportsPersonalInfo.setProjectId(projectId);
                        sportsCaseReport.setCaseType(getCaseType(apply.getType()));
                        sportsCaseReport.setProjectId(projectId);
                        sportsCaseReport.setCaseName(apply.getName());
                        sportsCaseReport.setIzActive(CommonConstant.COMMON_STATUS_N);
                        sportsProjectMapper.insert(sportsProject);
                        sportsPersonalInfoMapper.insert(sportsPersonalInfo);
                        sportsCaseReportMapper.insert(sportsCaseReport);

                        //将此用户在用户表和角色表中创建一条数据
                        SysUser sysUser = sysUserMapper.getUserByName(mobile);
                        if (null == sysUser) {
                            String userId = IdWorker.getIdStr();
                            sysUser = new SysUser();
                            sysUser.setUsername(mobile);
                            sysUser.setRealname(shenbaorenMap.get("name").toString());
                            sysUser.setId(userId);
                            String salt = oConvertUtils.randomGen(8);
                            sysUser.setSalt(salt);
                            String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), CommonConstant.DEFAULT_PWD, salt);
                            sysUser.setPassword(passwordEncode);
                            if (null != shenbaorenMap.get("email") && !"".equals(shenbaorenMap.get("email").toString())) {
                                sysUser.setEmail(shenbaorenMap.get("email").toString());
                            }
                            if (null != shenbaorenMap.get("mobile") && !"".equals(shenbaorenMap.get("mobile").toString())) {
                                sysUser.setPhone(shenbaorenMap.get("mobile").toString());
                            }
                            sysUser.setStatus(CommonConstant.STATUS_NORMAL);
                            sysUser.setDelFlag(CommonConstant.DEL_FLAG_0);
//                            if (null != shenbaorenMap.get("school_name") && !"".equals(shenbaorenMap.get("school_name").toString())) {
//                            sysUser.setCompName(shenbaorenMap.get("school_name").toString());
//                            }
                            sysUser.setCreateBy("admin");
                            sysUserMapper.insert(sysUser);

                            SysUserRole sysUserRole = new SysUserRole();
                            sysUserRole.setRoleId(getRoleId(apply.getIdentity()));
                            sysUserRole.setUserId(userId);
                            sysUserRoleMapper.insert(sysUserRole);
                        }
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                });
            }
            executor.shutdown();
            executor.awaitTermination(30, TimeUnit.HOURS); // 等待所有任务完成，或者根据需要调整等待时间
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("解析文件异常,请联系管理员！");
        }
        return Result.OK("解析完成！");
    }

    @Override
    @Transactional
    public Result<?> parseHisData() {
        log.info("-----------------------开始解析-------------------------");
        long startTime = System.currentTimeMillis();
        //1.读取JSO数据
        log.info("1.读取JSON文件");
        String JSON_PATH = "E:\\project\\NIKE项目\\2022-2023.json";
        File file = new File(JSON_PATH);
        try {
            String content = FileUtils.readFileToString(file, "UTF-8");
            List<SelectionApply> selectionApplies = parseArray(content, SelectionApply.class);
            ExecutorService executor = Executors.newFixedThreadPool(20);
            for (SelectionApply apply : selectionApplies) {
                executor.submit(() -> {
                    try {
                        String projectId = "NIKE" + apply.getYear() + apply.getId();
                        SportsProject sportsProject = new SportsProject();
                        SportsPersonalInfo sportsPersonalInfo = new SportsPersonalInfo();
                        sportsProject.setProjectName(apply.getName());
                        sportsProject.setId(projectId);
                        sportsProject.setPrize(apply.getPrize());
                        sportsProject.setCaseType(getCaseType(apply.getType()));
                        sportsProject.setYear(apply.getYear());
                        sportsProject.setAverageOrdinary(BigDecimal.valueOf(apply.getAverage()));
                        sportsProject.setRoleType(getRoleType(apply.getIdentity()));
                        if (!"".equals(apply.getContent_json())){
                            Map<String, Object> contentMap = (Map<String, Object>) JSONObject.parse(apply.getContent_json());
                            if (null != contentMap.get("school") && !"".equals(contentMap.get("school").toString())) {
                                String schoolName = contentMap.get("school").toString();
                                sportsProject.setSchoolName(schoolName);
                                sportsPersonalInfo.setSchoolName(schoolName);
                            }
                            sportsPersonalInfo.setProjectId(projectId);
                            sportsPersonalInfo.setProjectId(projectId);
                            if (null != contentMap.get("province") && !"".equals(contentMap.get("province").toString())) {
                                sportsPersonalInfo.setSchoolProvince(getAreaName(contentMap.get("province").toString(), CommonConstant.AREA_PROVINCE));
                            }
                            if (null != contentMap.get("county") && !"".equals(contentMap.get("county").toString())) {
                                sportsPersonalInfo.setSchoolArea(getAreaName(contentMap.get("county").toString(), CommonConstant.AREA_COUNTY));
                            }
                            if (null != contentMap.get("city") && !"".equals(contentMap.get("city").toString())) {
                                sportsPersonalInfo.setSchoolCity(getAreaName(contentMap.get("city").toString(), CommonConstant.AREA_CITY));
                            }
                            if (null != contentMap.get("address") && !"".equals(contentMap.get("address").toString())) {
                                sportsPersonalInfo.setSchoolAddress(contentMap.get("address").toString());
                            }
                            if (null != contentMap.get("student") && !"".equals(contentMap.get("student").toString())) {
                                sportsPersonalInfo.setSchoolStuNum(Integer.parseInt(contentMap.get("student").toString()));
                            }
                            if (null != contentMap.get("grade_class") && !"".equals(contentMap.get("grade_class").toString())) {
                                sportsPersonalInfo.setClassNum(Integer.parseInt(contentMap.get("grade_class").toString()));
                            }
                        }
                        if (null != apply.getXueduan() && !"".equals(apply.getXueduan())) {
                            sportsPersonalInfo.setSchoolStage(apply.getXueduan());
                        }

                        if (null != apply.getShenbaoren_json() && !"".equals(apply.getShenbaoren_json())) {
                            Map<String, Object> shenbaorenMap = (Map<String, Object>) JSONObject.parse(apply.getShenbaoren_json());
                            String mobile = "";
                            if (!"".equals(apply.getContent_json())) {
                                if (null != shenbaorenMap.get("mobile") && !"".equals(shenbaorenMap.get("mobile").toString())) {
                                    mobile = shenbaorenMap.get("mobile").toString();
                                }
                                if (null != shenbaorenMap.get("name") && !"".equals(shenbaorenMap.get("name").toString())) {
                                    sportsPersonalInfo.setName(shenbaorenMap.get("name").toString());
                                    sportsProject.setUserName(shenbaorenMap.get("name").toString());
                                }
                                if (null != shenbaorenMap.get("gender") && !"".equals(shenbaorenMap.get("gender").toString())) {
                                    // 解析出来的数据性别 2代表男，女代表1。
                                    String gender = "1".equals(shenbaorenMap.get("gender").toString()) ? "2" : "1";
                                    sportsPersonalInfo.setSex(Integer.parseInt(gender));
                                }
                                if (null != shenbaorenMap.get("nation") && !"".equals(shenbaorenMap.get("nation").toString())) {
                                    sportsPersonalInfo.setNation(shenbaorenMap.get("nation").toString());
                                }
                                sportsPersonalInfo.setPhone(mobile);
                                if (null != shenbaorenMap.get("email") && !"".equals(shenbaorenMap.get("email").toString())) {
                                    sportsPersonalInfo.setEmail(shenbaorenMap.get("email").toString());
                                }
                                if (null != shenbaorenMap.get("shenfenzheng") && !"".equals(shenbaorenMap.get("shenfenzheng").toString())) {
                                    sportsPersonalInfo.setIdNumber(shenbaorenMap.get("shenfenzheng").toString());
                                }
                                if (null != shenbaorenMap.get("zhiwei") && !"".equals(shenbaorenMap.get("zhiwei").toString())) {
                                    sportsPersonalInfo.setPosition(shenbaorenMap.get("zhiwei").toString());
                                }
                                if (null != shenbaorenMap.get("zhicheng") && !"".equals(shenbaorenMap.get("zhicheng").toString())) {
                                    sportsPersonalInfo.setProfessor(shenbaorenMap.get("zhicheng").toString());
                                }
                            }

                            sportsPersonalInfoMapper.insert(sportsPersonalInfo);
                            sportsProjectMapper.insert(sportsProject);

                            //将此用户在用户表和角色表中创建一条数据
                            SysUser sysUser = sysUserMapper.getUserByName(mobile);
                            if (null == sysUser) {
                                String userId = IdWorker.getIdStr();
                                sysUser = new SysUser();
                                sysUser.setUsername(mobile);
                                if (null != shenbaorenMap.get("name") && !"".equals(shenbaorenMap.get("name").toString())) {
                                    sysUser.setRealname(shenbaorenMap.get("name").toString());
                                }
                                sysUser.setId(userId);
                                String salt = oConvertUtils.randomGen(8);
                                sysUser.setSalt(salt);
                                String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), CommonConstant.DEFAULT_PWD, salt);
                                sysUser.setPassword(passwordEncode);
                                if (null != shenbaorenMap.get("email") && !"".equals(shenbaorenMap.get("email").toString())) {
                                    sysUser.setEmail(shenbaorenMap.get("email").toString());
                                }
                                if (null != shenbaorenMap.get("mobile") && !"".equals(shenbaorenMap.get("mobile").toString())) {
                                    sysUser.setPhone(shenbaorenMap.get("mobile").toString());
                                }
                                sysUser.setStatus(CommonConstant.STATUS_NORMAL);
                                sysUser.setDelFlag(CommonConstant.DEL_FLAG_0);
                                sysUser.setCreateBy("admin");
                                sysUserMapper.insert(sysUser);
                                SysUserRole sysUserRole = new SysUserRole();
                                sportsProject.setRoleType(getRoleId(apply.getIdentity()));
                                sysUserRole.setUserId(userId);
                                sysUserRoleMapper.insert(sysUserRole);
                            }
                        }


                        if (!"".equals(apply.getShenbaoshu_json())) {
                            Map<String, Object> map = (Map<String, Object>) JSONObject.parse(apply.getShenbaoshu_json());
                            SportsCaseReport sportsCaseReport = new SportsCaseReport();
                            sportsCaseReport.setProjectId(projectId);
                            if (null != map.get("name") && !"".equals(map.get("name").toString())) {
                                sportsCaseReport.setCaseName(map.get("name").toString());
                            }
                            if (null != map.get("xueduan") && !"".equals(map.get("xueduan").toString())) {
                                sportsCaseReport.setCourseStage(map.get("xueduan").toString());
                            }
                            StringBuffer caseContent = new StringBuffer();
                            if (null != map.get("s_1") && !"".equals(map.get("s_1").toString())) {
                                caseContent.append(map.get("s_1").toString());
                            }
                            if (null != map.get("s_2") && !"".equals(map.get("s_2").toString())) {
                                caseContent.append(map.get("s_2").toString());
                            }
                            if (null != map.get("s_3") && !"".equals(map.get("s_3").toString())) {
                                caseContent.append(map.get("s_3").toString());
                            }
                            if (null != map.get("s_4") && !"".equals(map.get("s_4").toString())) {
                                caseContent.append(map.get("s_4").toString());
                            }
                            if (null != map.get("s_5") && !"".equals(map.get("s_5").toString())) {
                                caseContent.append(map.get("s_5").toString());
                            }
                            if (null != map.get("s_6") && !"".equals(map.get("s_6").toString())) {
                                caseContent.append(map.get("s_6").toString());
                            }
                            if (null != map.get("s_7") && !"".equals(map.get("s_7").toString())) {
                                caseContent.append(map.get("s_7").toString());
                            }
                            if (null != map.get("s_8") && !"".equals(map.get("s_8").toString())) {
                                caseContent.append(map.get("s_8").toString());
                            }
                            if (null != map.get("s_9") && !"".equals(map.get("s_9").toString())) {
                                caseContent.append(map.get("s_9").toString());
                            }
                            if (null != map.get("s_10") && !"".equals(map.get("s_10").toString())) {
                                caseContent.append(map.get("s_10").toString());
                            }
                            if (null != map.get("s_11") && !"".equals(map.get("s_11").toString())) {
                                caseContent.append(map.get("s_11").toString());
                            }
                            if (null != map.get("s_12") && !"".equals(map.get("s_12").toString())) {
                                caseContent.append(map.get("s_12").toString());
                            }
                            if (null != map.get("s_13") && !"".equals(map.get("s_13").toString())) {
                                caseContent.append(map.get("s_13").toString());
                            }
                            sportsCaseReport.setCaseContent(caseContent.toString());
                            sportsCaseReport.setIzActive(CommonConstant.COMMON_STATUS_N);
                            sportsCaseReport.setCreateTime(new Date());
                            sportsCaseReport.setCreateBy("admin");
                            sportsCaseReportMapper.insert(sportsCaseReport);


                            if (null != apply.getZizhi() && !"".equals(apply.getZizhi())) {
                                parseZiZhi(apply.getZizhi(), projectId);
                            }

                            if (null != apply.getZizhi2() && !"".equals(apply.getZizhi2())) {
                                parseZiZhi(apply.getZizhi2(), projectId);
                            }
                            if (null != apply.getShipin() && !"".equals(apply.getShipin())) {
                                String shipin = apply.getShipin();
                                List<String> shipinList = new ArrayList<>();
                                shipinList.add(shipin);
                                setSportsProjectCertificate(shipinList, projectId, CommonConstant.OSS_FILE_TYPE_VIDEO);
                            }
                        }
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                });
            }
            executor.shutdown();
            executor.awaitTermination(30, TimeUnit.HOURS); // 等待所有任务完成，或者根据需要调整等待时间
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("解析文件异常,请联系管理员！");
        }
        return Result.OK("解析完成！");
    }

    public String getCaseType(String type){
        String caseType;
        switch (type) {
            case ("huodong"):
                caseType = CommonConstant.CASE_TYPE_ACTIVITY;
                break;
            case ("other"):
                caseType = CommonConstant.CASE_TYPE_OTHER;
                break;
            case ("ketang"):
                caseType = CommonConstant.CASE_TYPE_TEACH;
                break;
            case ("wenhua"):
                caseType = CommonConstant.CASE_TYPE_CULTURE_NAME;
                break;
            default:
                caseType = "";
        }
        return caseType;
    }

    public boolean existSportsProject(String projectId){
        LambdaQueryWrapper<SportsProject> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsProject::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsProject::getId, projectId);
        SportsProject sportsProject = sportsProjectMapper.selectOne(lambdaQueryWrapper);
        if (null != sportsProject){
            return true;
        }
        return false;
    }

    public void updateSchoolStage(SelectionApply selectionApply){
        if (!"".equals(selectionApply.getShenbaoren_json())) {
            String projectId = "NIKE" + selectionApply.getYear() + selectionApply.getId();
            SportsPersonalInfo sportsPersonalInfo = sportsPersonalInfoMapper.selectByProjectId(projectId);
            if (sportsPersonalInfo != null) {
//                Map<String, Object> obj = (Map) JSONObject.parse(selectionApply.getShenbaoren_json());
//                if (null != obj.get("school_period") && !"".equals(obj.get("school_period").toString())) {
//                    String schoolPeriod = obj.get("school_period").toString();
//                    sportsPersonalInfo.setSchoolStage(sysDictMapper.getDictItemByValue("school_stage", schoolPeriod));
//                }
//                String schoolType1 = obj.get("school_type1").toString();
//                sportsPersonalInfo.setSchoolType(sysDictMapper.getDictItemByValue("school_type", schoolType1));
//                String schoolType2 = obj.get("school_type2").toString();
//                sportsPersonalInfo.setSchoolLocation(sysDictMapper.getDictItemByValue("school_location", schoolType2));
//                sportsPersonalInfo.setSchoolPeriod(selectionApply.getPeriod());
//                sportsPersonalInfoMapper.updateById(sportsPersonalInfo);

                Map<String, Object> obj1 = (Map<String, Object>) JSONObject.parse(selectionApply.getWork_json());
                SportsWorkInfo sportsWorkInfo = getSportsWorkInfoByPID(projectId);
                if (null != sportsWorkInfo) {
                    //工作感知
                    if (null != obj1.get("feel") && !"".equals(obj1.get("feel").toString())) {
                        List<String> certificate = (List<String>) obj1.get("feel");
                        sportsWorkInfo.setWorkStrengthSelfEvalution(sysDictMapper.getDictItemByValue("work_strength_self_evalution", certificate.get(1)));
                        sportsWorkInfo.setWorkEnvironmentEvalution(sysDictMapper.getDictItemByValue("evalution_type", certificate.get(2)));
                        sportsWorkInfo.setWorkMotivationEvalution(sysDictMapper.getDictItemByValue("evalution_type", certificate.get(3)));
                        sportsWorkInfo.setWorkPositivitySelfEvalution(sysDictMapper.getDictItemByValue("evalution_type", certificate.get(4)));
                        sportsWorkInfoMapper.updateById(sportsWorkInfo);
                    }
                }
            }

        }
    }

    public SportsWorkInfo getSportsWorkInfoByPID(String projectId) {
        LambdaQueryWrapper<SportsWorkInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsWorkInfo::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsWorkInfo::getProjectId, projectId);
        return sportsWorkInfoMapper.selectOne(lambdaQueryWrapper);
    }

    @Transactional
    public void parse(SelectionApply selectionApply) throws Exception{
        String projectId = "NIKE" + selectionApply.getYear() + selectionApply.getId();
        //判断此projectId是否已经存在，不存则开始解析
        if (!existSportsProject(projectId)) {
            String userId = IdWorker.getIdStr();
            log.info("2.解析案例主表数据");
            SportsProject sportsProject = new SportsProject();
            sportsProject.setId(projectId);
            sportsProject.setProjectName(selectionApply.getName());
            sportsProject.setYear(selectionApply.getYear());
            sportsProject.setProjectStatus(CommonConstant.PROJECT_STATUS_SUBMIT);
            sportsProject.setFirstAuditStatus(CommonConstant.PROJECT_FIRST_AUDIT_STATUS_UNPASS);
            sportsProject.setProjectStep(CommonConstant.PROJECT_STEP_FINISH);
            sportsProject.setIzDispute(CommonConstant.COMMON_STATUS_N);
            sportsProject.setRoleType(CommonConstant.ROLE_TYPE_TE);
            sportsProject.setCaseType(getCaseType(selectionApply.getType()));
            sportsProject.setUserId(userId);
            sportsProject.setRoleId(getRoleId(selectionApply.getIdentity()));
            sportsProject.setIzActive("0");
            sportsProject.setRoleType(getRoleType(selectionApply.getIdentity()));
            sportsProject.setAverageOrdinary(BigDecimal.valueOf(selectionApply.getAverage()));
            //从申报人信息中查找学校名和提交人姓名
            if (null != selectionApply.getShenbaoren_json() && !"".equals(selectionApply.getShenbaoren_json())) {
                Map<String, Object> obj = (Map) JSONObject.parse(selectionApply.getShenbaoren_json());
                sportsProject.setUserName(obj.get("name").toString());
                if (null != obj.get("school_name") && !"".equals(obj.get("school_name"))) {
                    sportsProject.setSchoolName(obj.get("school_name").toString());
                }
            }
            sportsProjectMapper.insert(sportsProject);

            log.info("3.解析案例视频数据");
            if (null != selectionApply.getShipin() && !"".equals(selectionApply.getShipin())) {
                String shipin = selectionApply.getShipin();
                List<String> shipinList = new ArrayList<>();
                shipinList.add(shipin);
                setSportsProjectCertificate(shipinList, projectId, CommonConstant.OSS_FILE_TYPE_VIDEO);
            }

            log.info("4.解析案例对应的个人信息表");
            if (null != selectionApply.getShenbaoren_json()  && !"".equals(selectionApply.getShenbaoren_json())) {
                parseShenbaorenJson(selectionApply.getShenbaoren_json(), projectId, userId, selectionApply.getIdentity());
            }

            log.info("5.解析案例对应的学校工作展开情况");
            if (null != selectionApply.getWork_json() && !"".equals(selectionApply.getWork_json())) {
                parseWorkJson(selectionApply.getWork_json(), projectId, selectionApply.getIdentity());
            }

            log.info("6.解析资质文件报告");
            if (null != selectionApply.getZizhi() && !"".equals(selectionApply.getZizhi())) {
                parseZiZhi(selectionApply.getZizhi(), projectId);
            }

            log.info("7.解析案例申报书");
            if (null != selectionApply.getShenbaoshu_json() && !"".equals(selectionApply.getShenbaoshu_json())) {
                String shenbaoshu = selectionApply.getShenbaoshu_json();
                if (selectionApply.getIdentity() == 1) {
                    //一线体育老师
                    parseShenbaoshuJsonTe(shenbaoshu, projectId, selectionApply.getIdentity());
                } else if(selectionApply.getIdentity() == 2) {
                    //体育教研员  有zhiwu
                    parseShenbaoshuJsonTe(shenbaoshu, projectId, selectionApply.getIdentity());
                } else if(selectionApply.getIdentity() == 3) {
                    //校长
                    parseShenbaoshuJsonSM(shenbaoshu, projectId);
                } else if(selectionApply.getIdentity() == 4) {
                    //推广大使
                    parseShenbaoshuJsonPA(shenbaoshu, projectId);
                }
            }
            log.info("单个案例解析完成！！！");
        }
    }

    public String getRoleType(int identity){
        if (identity == 1) {
            return "1";
        }
        if (identity == 2) {
            return "4";
        }
        if (identity == 3) {
            return "3";
        }
        if (identity == 4) {
            return "2";
        }
        return "0";
    }

    public String getRoleId(int identity){
        if (identity == 1) {
            return "20241205001";
        }
        if (identity == 2) {
            return "20241205003";
        }
        if (identity == 3) {
            return "20241205002";
        }
        if (identity == 4) {
            return "20241205004";
        }
        return "0";
    }

    public void parseShenbaoshuJsonPA(String shenbaoshu, String projectId){
        Map<String, Object> map = (Map<String, Object>) JSONObject.parse(shenbaoshu);
        SportsCaseReport sportsCaseReport = new SportsCaseReport();
        sportsCaseReport.setProjectId(projectId);
        if (null != map.get("tuiguang") && !"".equals(map.get("tuiguang").toString())) {
            sportsCaseReport.setCaseContent(map.get("tuiguang").toString());
        }
        if (null != map.get("name") && !"".equals(map.get("name").toString())) {
            sportsCaseReport.setCaseName(map.get("name").toString());
        }
        if (null != map.get("jiazhi") && !"".equals(map.get("jiazhi").toString())) {
            sportsCaseReport.setImplementationResult(map.get("jiazhi").toString());
        }
        if (null != map.get("support") && !"".equals(map.get("support").toString())) {
            JSONObject studyObj = JSONObject.parseObject(map.get("support").toString());
            StringBuffer sb = new StringBuffer();
            sb.append(studyObj.get("宣传材料")).append(studyObj.get("教具器材")).append(studyObj.get("培训"));
            sportsCaseReport.setImplementationProcess(sb.toString());
        }
        sportsCaseReport.setIzActive(CommonConstant.COMMON_STATUS_N);
        sportsCaseReport.setCreateBy("admin");
        sportsCaseReport.setCreateTime(new Date());
        sportsCaseReportMapper.insert(sportsCaseReport);
    }

    /**
     * 校长申报书内容
     * @param shenbaoshu
     * @param projectId
     */
    @Transactional
    public void parseShenbaoshuJsonSM(String shenbaoshu, String projectId) {
        Map<String, Object> map = (Map<String, Object>) JSONObject.parse(shenbaoshu);
        SportsCaseReport sportsCaseReport = new SportsCaseReport();
        sportsCaseReport.setProjectId(projectId);
        if (null != map.get("name") && !"".equals(map.get("name").toString())) {
            sportsCaseReport.setCaseName(map.get("name").toString());
        }
        if (null != map.get("s_1") && !"".equals(map.get("s_1").toString())) {
            sportsCaseReport.setActivitySchoolUnderstand(map.get("s_1").toString());
        }
        if (null != map.get("role") && !"".equals(map.get("role").toString())) {
            sportsCaseReport.setPositionDesc(map.get("role").toString());
        }
        sportsCaseReportMapper.insert(sportsCaseReport);
        if (null != map.get("tuijian") && !"".equals(map.get("tuijian").toString())) {
            JSONObject tuijianObj = JSONObject.parseObject(map.get("tuijian").toString());

            List<SportsReference> list = new ArrayList<>();
            for (String key : tuijianObj.keySet()) {
                JSONObject innerObject = tuijianObj.getJSONObject(key);
//            JSONArray boxArray = innerObject.getJSONArray("1");
                if (key.equals("3")) {
                    Map<String, Object> map1 = (Map<String, Object>) JSONObject.parse(innerObject.get("1").toString());
                    SportsReference sportsReference = new SportsReference();
                    sportsReference.setCompanyName(map1.get("t1").toString());
                    sportsReference.setColleagueName(map1.get("t2").toString());
                    sportsReference.setPhone(map1.get("t3").toString());
                    sportsReference.setReason(map1.get("t4").toString());
                    sportsReference.setProjectId(projectId);
                    sportsReference.setCreateBy("admin");
                    sportsReference.setId(IdWorker.getIdStr());
                    list.add(sportsReference);
                    Map<String, Object> map2 = (Map<String, Object>) JSONObject.parse(innerObject.get("2").toString());
                    SportsReference sportsReference2 = new SportsReference();
                    sportsReference2.setCompanyName(map2.get("t1").toString());
                    sportsReference2.setColleagueName(map2.get("t2").toString());
                    sportsReference2.setPhone(map2.get("t3").toString());
                    sportsReference2.setReason(map2.get("t4").toString());
                    sportsReference2.setProjectId(projectId);
                    sportsReference2.setCreateBy("admin");
                    sportsReference2.setId(IdWorker.getIdStr());
                    list.add(sportsReference2);
                } else {
                    SportsReference sportsReference = new SportsReference();
                    sportsReference.setCompanyName(innerObject.get("t1").toString());
                    sportsReference.setColleagueName(innerObject.get("t2").toString());
                    sportsReference.setPhone(innerObject.get("t3").toString());
                    sportsReference.setReason(innerObject.get("t4").toString());
                    sportsReference.setProjectId(projectId);
                    sportsReference.setCreateBy("admin");
                    sportsReference.setId(IdWorker.getIdStr());
                    list.add(sportsReference);
                }
            }
            sportsReferenceMapper.batchInsert(list);
        }
        if (null != map.get("up") && !"".equals(map.get("up").toString())){
            JSONObject upObj = JSONObject.parseObject(map.get("up").toString());
            parseUp(upObj, projectId);
        }
    }

    public void parseUp(JSONObject upObj, String projectId){
        for (String key : upObj.keySet()) {
            if (key.equals("list")) {
                JSONArray listObj = upObj.getJSONArray(key);
                List<OssFile> ossFiles = new ArrayList<>();
                for (int j = 0; j < listObj.size(); j++) {
                    OssFile ossFile = new OssFile();
                    JSONObject innerObject = listObj.getJSONObject(j);
                    ossFile.setUrl(innerObject.get("url").toString());
                    ossFile.setFileName(innerObject.get("name").toString());
                    ossFiles.add(ossFile);
                }
                addOssFile(ossFiles, projectId, CommonConstant.OSS_FILE_TYPE_FILE);
            }
            if (key.equals("shipin")){
                List<String> list1 = new ArrayList<>();
                list1.add(upObj.get("shipin").toString());
                setSportsProjectCertificate(list1, projectId, CommonConstant.OSS_FILE_TYPE_VIDEO);
            }
        }
    }

    /**
     * 一线体育老师提交的申报书json串解析
     * @param shenbaoshu
     * @param projectId
     */
    @Transactional
    public void parseShenbaoshuJsonTe(String shenbaoshu, String projectId, int identity){
        Map<String, Object> map = (Map<String, Object>) JSONObject.parse(shenbaoshu);
        SportsCaseReport sportsCaseReport = new SportsCaseReport();
        sportsCaseReport.setProjectId(projectId);
        if (3 == identity || 2 == identity){
            sportsCaseReport.setCaseName(map.get("case_name").toString());
            if (null != map.get("documentary_title") && !"".equals(map.get("documentary_title").toString())) {
                sportsCaseReport.setDocumentTitle(map.get("documentary_title").toString());
            }
            if (null != map.get("up") && !"".equals(map.get("up").toString())) {
                JSONObject upObj = JSONObject.parseObject(map.get("up").toString());
                parseUp(upObj, projectId);
            }
        }
        if (1 == identity){
            if (null != map.get("name") && !"".equals(map.get("name").toString())) {
                sportsCaseReport.setCaseName(map.get("name").toString());
            }
            if (null != map.get("s_1") && !"".equals(map.get("s_1").toString())) {
                sportsCaseReport.setCaseContent(map.get("s_1").toString());
            }
            if (null != map.get("up") && !"".equals(map.get("up").toString())) {
                JSONObject upObj = JSONObject.parseObject(map.get("up").toString());
                parseUp(upObj, projectId);
            }
            if (null != map.get("type") && !"".equals(map.get("type").toString())) {
                sportsCaseReport.setCaseType(getCaseType(map.get("type").toString()));
            }
            if (null != map.get("huodong_tag") && !"".equals(map.get("huodong_tag").toString())) {
                sportsCaseReport.setOtherCase(getCaseType(map.get("huodong_tag").toString()));
            }
            if (null != map.get("period") && !"".equals(map.get("period").toString())) {
                sportsCaseReport.setCourseStage(sysDictMapper.getDictItemByValue("teach_stage", map.get("period").toString()));
            }
        }
        if (null != map.get("case_feature") && !"".equals(map.get("case_feature").toString())) {
            sportsCaseReport.setCaseFeatures(map.get("case_feature").toString());
        }
        if (null != map.get("case_challenges") && !"".equals(map.get("case_challenges").toString())) {
            sportsCaseReport.setProblemChallengeDesc(map.get("case_challenges").toString());
        }
        if (null != map.get("case_analyze") && !"".equals(map.get("case_analyze").toString())) {
            sportsCaseReport.setAnalysisSolutionDesc(map.get("case_analyze").toString());
        }
        if (null != map.get("case_progress") && !"".equals(map.get("case_progress").toString())) {
            sportsCaseReport.setImplementationProcess(map.get("case_progress").toString());
        }

        if (null != map.get("case_result") && !"".equals(map.get("case_result").toString())) {
            sportsCaseReport.setImplementationResult(map.get("case_result").toString());
        }
        if (null != map.get("case_review") && !"".equals(map.get("case_review").toString())) {
            sportsCaseReport.setCaseFeatures(map.get("case_review").toString());
        }
        if (null != map.get("case_reflect") && !"".equals(map.get("case_reflect").toString())) {
            sportsCaseReport.setImprovementDesc(map.get("case_reflect").toString());
        }
        sportsCaseReport.setIzActive(CommonConstant.COMMON_STATUS_N);
        sportsCaseReportMapper.insert(sportsCaseReport);

        SportsReference sportsReference1 = new SportsReference();
        sportsReference1.setProjectId(projectId);
        if (null != map.get("referrer_unit1") && !"".equals(map.get("referrer_unit1").toString())) {
            sportsReference1.setCompanyName(map.get("referrer_unit1").toString());
        }
        if (null != map.get("referrer_name1") && !"".equals(map.get("referrer_name1").toString())) {
            sportsReference1.setColleagueName(map.get("referrer_name1").toString());
        }
        if (null != map.get("referrer_phone1") && !"".equals(map.get("referrer_phone1").toString())) {
            sportsReference1.setPhone(map.get("referrer_phone1").toString());
        }
        sportsReference1.setIzActive(CommonConstant.COMMON_STATUS_N);
        sportsReference1.setColleagueType(CommonConstant.COLLEAGUE_TYPE_TE);
        sportsReferenceMapper.insert(sportsReference1);

        SportsReference sportsReference2 = new SportsReference();
        sportsReference2.setProjectId(projectId);
        if (null != map.get("referrer_unit2") && !"".equals(map.get("referrer_unit2").toString())) {
            sportsReference2.setCompanyName(map.get("referrer_unit2").toString());
        }
        if (null != map.get("referrer_name2") && !"".equals(map.get("referrer_name2").toString())) {
            sportsReference2.setColleagueName(map.get("referrer_name2").toString());
        }
        if (null != map.get("referrer_phone2") && !"".equals(map.get("referrer_phone2").toString())) {
            sportsReference2.setPhone(map.get("referrer_phone2").toString());
        }
        sportsReference2.setIzActive(CommonConstant.COMMON_STATUS_N);
        sportsReference2.setColleagueType(CommonConstant.COLLEAGUE_TYPE_TE);
        sportsReferenceMapper.insert(sportsReference2);

        SportsReference sportsReference3 = new SportsReference();
        sportsReference3.setProjectId(projectId);
        if (null != map.get("referrer_unit3") && !"".equals(map.get("referrer_unit3").toString())) {
            sportsReference3.setCompanyName(map.get("referrer_unit3").toString());
        }
        if (null != map.get("referrer_name3") && !"".equals(map.get("referrer_name3").toString())) {
            sportsReference3.setColleagueName(map.get("referrer_name3").toString());
        }
        if (null != map.get("referrer_phone3") && !"".equals(map.get("referrer_phone3").toString())) {
            sportsReference3.setPhone(map.get("referrer_phone3").toString());
        }
        sportsReference3.setIzActive(CommonConstant.COMMON_STATUS_N);
        sportsReference3.setColleagueType(CommonConstant.COLLEAGUE_TYPE_MASTER);
        sportsReferenceMapper.insert(sportsReference3);

        SportsReference sportsReference4 = new SportsReference();
        sportsReference4.setProjectId(projectId);
        if (null != map.get("referrer_unit4") && !"".equals(map.get("referrer_unit4").toString())) {
            sportsReference4.setCompanyName(map.get("referrer_unit4").toString());
        }
        if (null != map.get("referrer_name4") && !"".equals(map.get("referrer_name4").toString())) {
            sportsReference4.setColleagueName(map.get("referrer_name4").toString());
        }
        if (null != map.get("referrer_phone4") && !"".equals(map.get("referrer_phone4").toString())) {
            sportsReference4.setPhone(map.get("referrer_phone4").toString());
        }
        sportsReference4.setIzActive(CommonConstant.COMMON_STATUS_N);
        sportsReference4.setColleagueType(CommonConstant.COLLEAGUE_TYPE_MASTER);
        sportsReferenceMapper.insert(sportsReference4);

        SportsReference sportsReference5= new SportsReference();
        sportsReference5.setProjectId(projectId);
        if (null != map.get("referrer_unit5") && !"".equals(map.get("referrer_unit5").toString())) {
            sportsReference5.setCompanyName(map.get("referrer_unit5").toString());
        }
        if (null != map.get("referrer_name5") && !"".equals(map.get("referrer_name5").toString())) {
            sportsReference5.setColleagueName(map.get("referrer_name5").toString());
        }
        if (null != map.get("referrer_phone5") && !"".equals(map.get("referrer_phone5").toString())) {
            sportsReference5.setPhone(map.get("referrer_phone5").toString());
        }
        sportsReference5.setIzActive(CommonConstant.COMMON_STATUS_N);
        sportsReference5.setColleagueType(CommonConstant.COLLEAGUE_TYPE_LEADER);
        sportsReferenceMapper.insert(sportsReference5);
    }

    @Transactional
    public void parseShenbaorenJson(String shenbaoren, String projectId, String userId, int identity) throws Exception{
        Map<String, Object> obj = (Map) JSONObject.parse(shenbaoren);
        SportsPersonalInfo sportsPersonalInfo = new SportsPersonalInfo();
        sportsPersonalInfo.setProjectId(projectId);
        sportsPersonalInfo.setName(obj.get("name").toString());
        // 解析出来的数据性别 2代表男，女代表1。
        String gender = "1".equals(obj.get("gender").toString()) ? "2" : "1";
        sportsPersonalInfo.setSex(Integer.parseInt(gender));
        if (null != obj.get("school_name") && !"".equals(obj.get("school_name").toString())) {
            sportsPersonalInfo.setSchoolName(obj.get("school_name").toString());
        }
        if (null != obj.get("school_address") && !"".equals(obj.get("school_address").toString())) {
            sportsPersonalInfo.setSchoolAddress(obj.get("school_address").toString());
        }
        if (null != obj.get("nation") && !"".equals(obj.get("nation").toString())) {
            sportsPersonalInfo.setNation(obj.get("nation").toString());
        }
        if (null != obj.get("birthday") && !"".equals(obj.get("birthday").toString())) {
            sportsPersonalInfo.setBirthday(obj.get("birthday").toString());
        }
        if (null != obj.get("province") && !"".equals(obj.get("province").toString())) {
            sportsPersonalInfo.setSchoolProvince(getAreaName(obj.get("province").toString(), CommonConstant.AREA_PROVINCE));
        }
        if (null != obj.get("county") && !"".equals(obj.get("county").toString())) {
            sportsPersonalInfo.setSchoolArea(getAreaName(obj.get("county").toString(), CommonConstant.AREA_COUNTY));
        }
        if (null != obj.get("city") && !"".equals(obj.get("city").toString())) {
            sportsPersonalInfo.setSchoolCity(getAreaName(obj.get("city").toString(), CommonConstant.AREA_CITY));
        }
        if (null != obj.get("shenfenzheng") && !"".equals(obj.get("shenfenzheng").toString())) {
            sportsPersonalInfo.setIdNumber(obj.get("shenfenzheng").toString());
        }
        if (null != obj.get("class_num") && "".equals(obj.get("class_num").toString())) {
            sportsPersonalInfo.setClassNum(Integer.parseInt(obj.get("class_num").toString()));
        }
        if (null != obj.get("student_num") && "".equals(obj.get("student_num").toString())) {
            sportsPersonalInfo.setSchoolStuNum(Integer.parseInt(obj.get("student_num").toString()));
        }

        if (null != obj.get("teacher_num1") && !"".equals(obj.get("teacher_num1").toString())) {
            sportsPersonalInfo.setUnderSportTeachersNum(Integer.parseInt(obj.get("teacher_num1").toString()));
        }
        if (null != obj.get("teacher_num2") && !"".equals(obj.get("teacher_num2").toString())) {
            sportsPersonalInfo.setTemporarySportTeachersNum(Integer.parseInt(obj.get("teacher_num2").toString()));
        }
        if (null != obj.get("teacher_num3") && !"".equals(obj.get("teacher_num3").toString())) {
            sportsPersonalInfo.setNationalGradeTeachersNum(Integer.parseInt(obj.get("teacher_num3").toString()));
        }
        if (null != obj.get("teacher_num4") && !"".equals(obj.get("teacher_num4").toString())) {
            sportsPersonalInfo.setProvinceGradeTeachersNum(Integer.parseInt(obj.get("teacher_num4").toString()));
        }
        if (null != obj.get("capita_area") && !"".equals(obj.get("capita_area").toString())){
            sportsPersonalInfo.setStuSportArea(obj.get("capita_area").toString());
        }


        if (null != obj.get("email") && !"".equals(obj.get("email").toString())) {
            sportsPersonalInfo.setEmail(obj.get("email").toString());
        }
        if (null != obj.get("special") && !"".equals(obj.get("special").toString())) {
            String specialSchool = obj.get("special").toString();
            sportsPersonalInfo.setIzSpecialSchool(specialSchool.equals("否") ? "0" : "1");
        }
        if (null != obj.get("is_full") && !"".equals(obj.get("is_full").toString())) {
            sportsPersonalInfo.setIzFullTime(obj.get("is_full").toString().equals("否") ? "0" : "1");
        }
        if (null != obj.get("school_type1") && !"".equals(obj.get("school_type1").toString())) {
            String schoolType1 = obj.get("school_type1").toString();
            sportsPersonalInfo.setSchoolType(sysDictMapper.getDictItemByValue("school_type", schoolType1));
        }
        if (null != obj.get("school_type2") && !"".equals(obj.get("school_type2").toString())) {
            String schoolType2 = obj.get("school_type2").toString();
            sportsPersonalInfo.setSchoolLocation(sysDictMapper.getDictItemByValue("school_location", schoolType2));
        }
        if (null !=obj.get("school_period") && !"".equals(obj.get("school_period").toString())) {
            String schoolPeriod = obj.get("school_period").toString();
            sportsPersonalInfo.setSchoolStage(sysDictMapper.getDictItemByValue("school_stage", schoolPeriod));
        }
        if (null !=obj.get("years") && !"".equals(obj.get("years").toString())) {
            sportsPersonalInfo.setYearOfPe(Integer.parseInt(obj.get("years").toString()));
        }

        if (null !=obj.get("mobile") && !"".equals(obj.get("mobile").toString())) {
            sportsPersonalInfo.setPhone(obj.get("mobile").toString());
        }
        if (null !=obj.get("zhicheng") && !"".equals(obj.get("zhicheng").toString())) {
            sportsPersonalInfo.setProfessor(obj.get("zhicheng").toString());
        }

        if (null !=obj.get("zhiwu") && !"".equals(obj.get("zhiwu").toString())) {
            sportsPersonalInfo.setPosition(obj.get("zhiwu").toString());
        }
        if (null !=obj.get("source") && !"".equals(obj.get("source").toString())) {
            sportsPersonalInfo.setActivityChannel(obj.get("source").toString());
        }
        if (null != obj.get("tiyuxiangmu") && !"".equals(obj.get("tiyuxiangmu").toString())) {
            sportsPersonalInfo.setSportsEvent(obj.get("tiyuxiangmu").toString());
        }
        if (null !=obj.get("awards") && !"".equals(obj.get("awards").toString())) {
            sportsPersonalInfo.setPersonalHonor(obj.get("awards").toString());
        }
        if (null != obj.get("social_title") && !"".equals(obj.get("social_title").toString())) {
            sportsPersonalInfo.setSocialPosition(obj.get("social_title").toString());
        }
        if (null != obj.get("result") && !"".equals(obj.get("result").toString())) {
            sportsPersonalInfo.setRepresentativeResult(obj.get("result").toString());
        }
        if (null != obj.get("result_cover") && !"".equals(obj.get("result_cover").toString())) {
            sportsPersonalInfo.setRepresentativeResult(obj.get("result_cover").toString());
        }
        sportsPersonalInfoMapper.insert(sportsPersonalInfo);

        //将此用户在用户表和角色表中创建一条数据
        String mobile = "";
        if (null != obj.get("mobile") && !"".equals(obj.get("mobile").toString())) {
            mobile = obj.get("mobile").toString();
            SysUser sysUser = sysUserMapper.getUserByName(mobile);
            if (null == sysUser) {
                sysUser = new SysUser();
                sysUser.setUsername(obj.get("mobile").toString());
                if (null != obj.get("name") && !"".equals(obj.get("name").toString())) {
                    sysUser.setRealname(obj.get("name").toString());
                }
                sysUser.setId(userId);
                if (null != obj.get("birthday") && !"".equals(obj.get("birthday").toString())) {
                    sysUser.setBirthday(java.sql.Date.valueOf(obj.get("birthday").toString()));
                }
                if (null != obj.get("email") && !"".equals(obj.get("email").toString())) {
                    sysUser.setEmail(obj.get("email").toString());
                }
                sysUser.setPhone(obj.get("mobile").toString());
                sysUser.setSex(Integer.parseInt(gender));
                sysUser.setStatus(1);
                sysUser.setDelFlag(CommonConstant.DEL_FLAG_0);
                if (null != obj.get("school_name") && !"".equals(obj.get("school_name").toString())) {
                    sysUser.setCompName(obj.get("school_name").toString());
                }
                String salt = oConvertUtils.randomGen(8);
                sysUser.setSalt(salt);
                String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), CommonConstant.DEFAULT_PWD, salt);
                sysUser.setPassword(passwordEncode);
                sysUser.setCreateBy("admin");
                sysUserMapper.insert(sysUser);

                SysUserRole sysUserRole = new SysUserRole();
                //一线体育老师
                sysUserRole.setRoleId(getRoleId(identity));
                sysUserRole.setUserId(userId);
                sysUserRoleMapper.insert(sysUserRole);
            }
        }

        //以往工作履历
        if (null != obj.get("job_resume") && !"".equals(obj.get("job_resume").toString())) {
            setSportsProjectWorkExperience(obj.get("job_resume").toString(), projectId);
        }
        //荣誉证书
        if (identity == 4) {
            if (null != obj.get("result_cover") && !"".equals(obj.get("result_cover").toString())) {
                List<String> certificate = (List<String>) obj.get("result_cover");
                if (null != certificate && certificate.size() > 0) {
                    setSportsProjectCertificate(certificate, projectId, CommonConstant.OSS_FILE_TYPE_HONOR);
                }
            }
        } else {
            if (null != obj.get("certificate") && !"".equals(obj.get("certificate").toString())) {
                List<String> certificate = (List<String>) obj.get("certificate");
                if (null != certificate && certificate.size() > 0) {
                    setSportsProjectCertificate(certificate, projectId, CommonConstant.OSS_FILE_TYPE_HONOR);
                }
            }
        }
        //个人教育经历
        if (null != obj.get("school1") && !"".equals(obj.get("school1").toString())) {
            SportsPersonalEdu highSchool = new SportsPersonalEdu();
            highSchool.setSchoolName(obj.get("school1").toString());
            highSchool.setEduStage("1");
            highSchool.setProjectId(projectId);
            sportsPersonalEduMapper.insert(highSchool);
        }
        if (null != obj.get("school2") && !"".equals(obj.get("school2").toString())) {
            SportsPersonalEdu university = new SportsPersonalEdu();
            university.setSchoolName(obj.get("school2").toString());
            university.setMajorName(null != obj.get("major2") ? obj.get("major2").toString() : "");
            university.setEduStage("2");
            university.setProjectId(projectId);
            sportsPersonalEduMapper.insert(university);
        }
        if (null != obj.get("school3") && !"".equals(obj.get("school3").toString())){
            SportsPersonalEdu postgraduate = new SportsPersonalEdu();
            postgraduate.setSchoolName(obj.get("school3").toString());
            postgraduate.setMajorName(null != obj.get("school3")? obj.get("school3").toString() : "");
            postgraduate.setEduStage("3");
            postgraduate.setProjectId(projectId);
            sportsPersonalEduMapper.insert(postgraduate);
        }
    }

    public List<SportsProjectFile> getSportsProjectFiles(String projectId){
        LambdaQueryWrapper<SportsProjectFile> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SportsProjectFile::getProjectId, projectId);
        lambdaQueryWrapper.eq(SportsProjectFile::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsProjectFile::getFileType, CommonConstant.OSS_FILE_TYPE_VIDEO);
        return sportsProjectFileMapper.selectList(lambdaQueryWrapper);
    }

    public OssFile getOssFile(String ossFileId){
        LambdaQueryWrapper<OssFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OssFile::getId, ossFileId);
        return ossFileMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 案例中对应的文件写入上传文件的表中
     * @param certificate   url字符串list
     * @param projectId  案例ID
     * @param fileType   文件类型： 1-图片，2-视频，3-荣誉证书，4-职称证书
     */
    @Transactional
    public void setSportsProjectCertificate(List<String> certificate, String projectId, String fileType){
//        List<SportsProjectFile> sportsProjectFiles = new ArrayList<>();
//        List<OssFile> ossFiles = new ArrayList<>();
        if (null != certificate && certificate.size() > 0) {
            certificate.forEach(x -> {
                List<SportsProjectFile> list =  getSportsProjectFiles(projectId);
                if (null != list && list.size() > 0) {
                    String ossFileId = list.get(0).getOssFileId();
                    OssFile ossFile = getOssFile(ossFileId);
                    String url = x;
                    if (null == ossFile){
                        ossFile = new OssFile();
                        if (!"".equals(url) && url.length() > 10) {
                            ossFile.setFileName(url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".")));
                        }
                        //视频转换后的地址，待确定具体地址后再更新
                        ossFile.setUrl(url);
                        ossFile.setId(ossFileId);
                        ossFileMapper.insert(ossFile);
                    } else {
                        if (!"".equals(url) && url.length() > 10) {
                            ossFile.setFileName(url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".")));
                        }
                        //视频转换后的地址，待确定具体地址后再更新
                        ossFile.setUrl(url);
                        ossFileMapper.updateById(ossFile);
                    }
                }

//                String ossFileId = IdWorker.getIdStr();
//                SportsProjectFile sportsProjectFile = new SportsProjectFile();
//                sportsProjectFile.setId(ossFileId);
//                sportsProjectFile.setProjectId(projectId);
//                sportsProjectFile.setOssFileId(ossFileId);
//                sportsProjectFile.setFileType(fileType);
//                sportsProjectFileMapper.insert(sportsProjectFile);

//                OssFile ossFile = new OssFile();
//                ossFile.setId(ossFileId);
//                String url = x;
//                if (!"".equals(url) && url.length() > 10) {
//                    ossFile.setFileName(url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".")));
//                }
//                if (CommonConstant.OSS_FILE_TYPE_VIDEO.equals(fileType)) {
//                    //视频转换后的地址，待确定具体地址后再更新
//                    ossFile.setUrl(url);
//                } else {
//                    if (!"".equals(url) && url.length() > 10) {
//                        ossFile.setUrl(url.replace("http://www.huolixiaoyuan.com/upload", "https://nike-huolixiaoyuan.oss-rg-china-mainland.aliyuncs.com/rz_uploads"));
//                    }
//                }
//                ossFiles.add(ossFile);
//                ossFileMapper.insert(ossFile);

//                sportsProjectFiles.add(sportsProjectFile);
            });
        }
//        sportsProjectFileMapper.batchInsert(sportsProjectFiles);
//        ossFileMapper.batchInsert(ossFiles);
    }

    @Transactional
    public void addOssFile(List<OssFile> ossFileList, String projectId, String fileType){
        List<SportsProjectFile> sportsProjectFiles = new ArrayList<>();
        List<OssFile> ossFiles = new ArrayList<>();
        if (null != ossFileList && ossFileList.size() > 0) {
            ossFileList.forEach(x -> {
                OssFile ossFile = new OssFile();
                String ossFileId = IdWorker.getIdStr();
                ossFile.setId(IdWorker.getIdStr());
                ossFile.setFileName(x.getFileName());
                String url = x.getUrl();
                if (CommonConstant.OSS_FILE_TYPE_VIDEO.equals(fileType)) {
                    //视频转换后的地址，待确定具体地址后再更新
                    ossFile.setUrl(url);
                } else {
                    if (!"".equals(url) && url.length() > 10) {
                        ossFile.setUrl(url.replace("http://www.huolixiaoyuan.com/upload", "https://nike-huolixiaoyuan.oss-rg-china-mainland.aliyuncs.com/rz_uploads"));
                    }
                }
                ossFiles.add(ossFile);
//                ossFileMapper.insert(ossFile);
                SportsProjectFile sportsProjectFile = new SportsProjectFile();
                sportsProjectFile.setId(IdWorker.getIdStr());
                sportsProjectFile.setProjectId(projectId);
                sportsProjectFile.setOssFileId(ossFileId);
                sportsProjectFile.setFileType(fileType);
//                sportsProjectFileMapper.insert(sportsProjectFile);
                sportsProjectFiles.add(sportsProjectFile);
            });
        }
        sportsProjectFileMapper.batchInsert(sportsProjectFiles);
        ossFileMapper.batchInsert(ossFiles);
    }
    /**
     * 案例对应的个人工作经验
     * @param projectId
     */
    public void setSportsProjectWorkExperience(String jobResume, String projectId){
        Map<String,Object> map = JSONObject.parseObject(jobResume);
        List<SportsPersonalWorkExperience> list = new ArrayList<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            SportsPersonalWorkExperience workExperience = new SportsPersonalWorkExperience();
            String value =  entry.getValue().toString();
            Map<String, Object> map1 = (Map<String, Object>) JSONObject.parse(value);
            workExperience.setId(IdWorker.getIdStr());
            workExperience.setProjectId(projectId);
            workExperience.setCompanyName(map1.get("t1").toString());
            workExperience.setPosition(map1.get("t2").toString());
            workExperience.setStartDate(map1.get("t3").toString());
            workExperience.setEndDate(map1.get("t4").toString());
            list.add(workExperience);
        }
        sportsPersonalWorkExperienceMapper.batchInsert(list);
    }

    /**
     * 案例中省市区id转名称
     * @param id
     * @param key
     * @return
     */
    public String getAreaName(String id, String key) {
        List<AreaObj> areaObjs = (List<AreaObj>) redisUtil.get(key);
        for (int i = 0; i < areaObjs.size(); i++){
            AreaObj areaObj = areaObjs.get(i);
            if (id.equals(areaObj.getId())) {
                return areaObj.getName();
            }
        }
        return "";
    }

    //校长
    public void parseShenbaoshuJsonMaster(String shenbaoshu, String projectId){

    }

    public void parseZiZhi(String zizhi, String projectId){
        if (!"".equals(zizhi) && null != zizhi){
            SportsQualificationDoc qualificationDoc = new SportsQualificationDoc();
            qualificationDoc.setIzActive(CommonConstant.COMMON_STATUS_N);
            qualificationDoc.setFileUrl(zizhi.replace("http://nmg.tiyujiaoxue.cn/upload", "https://nike-huolixiaoyuan.oss-rg-china-mainland.aliyuncs.com/rz_uploads"));
            qualificationDoc.setProjectId(projectId);
            qualificationDoc.setFileName(zizhi.substring(zizhi.lastIndexOf("/")+1 , zizhi.lastIndexOf(".")));
            sportsQualificationDocMapper.insert(qualificationDoc);
        }
    }

    public void parseWorkJson(String work, String projectId, int identify) {
        log.info("projectId == {}", projectId);
        Map<String, Object> obj = (Map<String, Object>) JSONObject.parse(work);
        SportsWorkInfo sportsWorkInfo = new SportsWorkInfo();
        sportsWorkInfo.setProjectId(projectId);
        sportsWorkInfo.setIzActive(CommonConstant.COMMON_STATUS_N);

        if(identify == 3) {
            JSONObject workObj = JSONObject.parseObject(obj.get("work").toString());
            for (String key : workObj.keySet()) {
                if ("3".equals(key)||"4".equals(key)||"5".equals(key)||"6".equals(key)||"7".equals(key)||"8".equals(key)||"9".equals(key)||"10".equals(key)){
                    log.info(workObj.get(key).toString());
                }
            }
        }

        if(identify == 2) {
            if (null != obj.get("is_full") && !"".equals(obj.get("is_full").toString())){
                JSONArray array = JSONArray.parseArray(obj.get("is_full").toString());
                sportsWorkInfo.setWorkStrengthSelfEvalution(sysDictMapper.getDictItemByValue("work_strength_self_evalution", array.get(1).toString()));
                sportsWorkInfo.setWorkEnvironmentEvalution(sysDictMapper.getDictItemByValue("evalution_type", array.get(2).toString()));
                sportsWorkInfo.setWorkMotivationEvalution(sysDictMapper.getDictItemByValue("evalution_type", array.get(3).toString()));
                sportsWorkInfo.setWorkPositivitySelfEvalution(sysDictMapper.getDictItemByValue("evalution_type", array.get(4).toString()));
            }
            JSONObject studyObj = JSONObject.parseObject(obj.get("study_data").toString());
            for (String key : studyObj.keySet()) {
                Map<String, Object> studyMap = (Map<String, Object>) JSONObject.parse(studyObj.get("1").toString());
            }
        }
        //个人日常工作情况
        if (identify == 1) {
            JSONObject workObj = JSONObject.parseObject(obj.get("work").toString());
            for (String key : workObj.keySet()) {
                JSONObject innerObject = workObj.getJSONObject(key);
                if ("1".equals(key)) {
                    String is = innerObject.getString("is");
                    JSONArray boxArray = innerObject.getJSONArray("box");
                    for (int j = 0; j < boxArray.size(); j++) {
                        JSONObject boxObject = boxArray.getJSONObject(j);
                        Map<String, Object> boxMap = boxObject.getInnerMap();
                        sportsWorkInfo.setSportCourseStage(xueduanCode(boxMap.get("xueduan").toString()));
                        if (null != boxMap.get("num") && !"".equals(boxMap.get("num").toString())) {
                            sportsWorkInfo.setSportCourseNum(Integer.parseInt(boxMap.get("num").toString()));
                        }
                    }
                } else if ("2".equals(key)) {
                    String is = innerObject.getString("is");
                    JSONArray boxArray = innerObject.getJSONArray("box");
                    for (int j = 0; j < boxArray.size(); j++) {
                        JSONObject boxObject = boxArray.getJSONObject(j);
                        Map<String, Object> boxMap = boxObject.getInnerMap();
                        sportsWorkInfo.setHealthCourseStage(xueduanCode(boxMap.get("xueduan").toString()));
                        if (null != boxMap.get("num") && !"".equals(boxMap.get("num").toString())) {
                            sportsWorkInfo.setHealthCourseNum(Integer.parseInt(boxMap.get("num").toString()));
                        }
                    }
                } else if ("3".equals(key)) {
                    String is = innerObject.getString("is");
                    JSONArray boxArray = innerObject.getJSONArray("box");
                    for (int j = 0; j < boxArray.size(); j++) {
                        JSONObject boxObject = boxArray.getJSONObject(j);
                        Map<String, Object> boxMap = boxObject.getInnerMap();
                        sportsWorkInfo.setOtherCourse(xueduanCode(boxMap.get("xueduan").toString()));
                        if (null != boxMap.get("num") && !"".equals(boxMap.get("num").toString())) {
                            sportsWorkInfo.setOtherCourseNum(Integer.parseInt(boxMap.get("num").toString()));
                        }
                    }
                } else if ("4".equals(key)) {
                    String c = innerObject.getString("c");
                    String s = innerObject.getString("s");
                    if (null != c && !"".equals(c)) {
                        sportsWorkInfo.setTeachClassNum(Integer.parseInt(c));
                    }
                    if (null != s && !"".equals(s)) {
                        sportsWorkInfo.setTeachClassStuNum(Integer.parseInt(s));
                    }
                } else if ("5".equals(key)) {
                    String is = innerObject.getString("is");
                    String way = innerObject.getString("way");
                    sportsWorkInfo.setIzJoinSportHomework(getYesOrNo(is));
                    sportsWorkInfo.setJoinStyle(way);
                } else if ("6".equals(key)) {
                    String is = innerObject.getString("is");
                    String num = innerObject.getString("num");
                    sportsWorkInfo.setIzLeadRecess(getYesOrNo(is));
                    if (!"".equals(num)) {
                        sportsWorkInfo.setLeadRecessNum(Integer.parseInt(num));
                    }
                } else if ("7".equals(key)) {
                    String is = innerObject.getString("is");
                    JSONArray boxArray = innerObject.getJSONArray("box");
                    for (int j = 0; j < boxArray.size(); j++) {
                        JSONObject boxObject = boxArray.getJSONObject(j);
                        Map<String, Object> boxMap = boxObject.getInnerMap();
//                    sportsWorkInfo.setIzJoinAfterSchoolCare(boxMap.get("tag").toString());
                        if (null != boxMap.get("num") && !"".equals(boxMap.get("num").toString())) {
                            sportsWorkInfo.setAfterSchoolCareNum(Integer.parseInt(boxMap.get("num").toString()));
                        }
                    }
                } else if ("8".equals(key)) {
                    String is = innerObject.getString("is");
                    String num = innerObject.getString("num");
                    sportsWorkInfo.setIzLeadSportAssociation(getYesOrNo(is));
                    if (!"".equals(num)) {
                        sportsWorkInfo.setSportAssociationNum(Integer.parseInt(num));
                    }
                } else if ("9".equals(key)) {
                    String is = innerObject.getString("is");
                    JSONArray boxArray = innerObject.getJSONArray("box");
                    for (int j = 0; j < boxArray.size(); j++) {
                        JSONObject boxObject = boxArray.getJSONObject(j);
                        Map<String, Object> boxMap = boxObject.getInnerMap();
                        sportsWorkInfo.setSchoolSportEventName(boxMap.get("tag").toString());
                        if (null != boxMap.get("num") && !"".equals(boxMap.get("num").toString())) {
                            sportsWorkInfo.setSchoolSportEventNum(Integer.parseInt(boxMap.get("num").toString()));
                        }
                    }
                } else if ("10".equals(key)) {
                    String is = innerObject.getString("is");
                    String num = innerObject.getString("num");
                    String role = innerObject.getString("role");
                    sportsWorkInfo.setIzLeadSchoolSportEventOrg(getYesOrNo(is));
                    sportsWorkInfo.setSchoolSportEventOrgName(role);
                    if (!"".equals(num)) {
                        sportsWorkInfo.setSchoolSportEventOrgNum(Integer.parseInt(num));
                    }
                } else if ("11".equals(key)) {
                    String is = innerObject.getString("is");
                    JSONArray boxArray = innerObject.getJSONArray("box");
                    if (null != boxArray && boxArray.size() > 0) {
                        for (int j = 0; j < boxArray.size(); j++) {
                            JSONObject boxObject = boxArray.getJSONObject(j);
                            Map<String, Object> boxMap = boxObject.getInnerMap();
                            boxMap.get("tag").toString();
                            boxMap.get("role").toString();
                            boxMap.get("num1").toString();
                            boxMap.get("num2").toString();
                        }
                    }
                } else {
                    break;
                }
            }
        }
        //个人培训情况
        if (null != obj.get("train") && !"".equals(obj.get("train").toString())){
            JSONObject trainObject = JSONObject.parseObject(obj.get("train").toString());
            for (String key : trainObject.keySet()) {
                JSONObject innerObject = trainObject.getJSONObject(key);
                if ("1".equals(key)) {
                    sportsWorkInfo.setIzSchoolTrain(getYesOrNo(innerObject.getString("is")));
                    sportsWorkInfo.setSchoolTrainContent(innerObject.getString("content"));
                    if (null != innerObject.getString("num") && !"".equals(innerObject.getString("num"))) {
                        sportsWorkInfo.setSchoolTrainNum(Integer.parseInt(innerObject.getString("num")));
                    }
                }
                if ("2".equals(key)) {
                    sportsWorkInfo.setIzAreaTrain(getYesOrNo(innerObject.getString("is")));
                    sportsWorkInfo.setAreaTrainContent(innerObject.getString("content"));
                    if (null != innerObject.getString("num") && !"".equals(innerObject.getString("num"))) {
                        sportsWorkInfo.setAreaTrainNum(Integer.parseInt(innerObject.getString("num")));
                    }
                }
                if ("3".equals(key)) {
                    sportsWorkInfo.setIzCityTrain(getYesOrNo(innerObject.getString("is")));
                    sportsWorkInfo.setCityTrainContent(innerObject.getString("content"));
                    if (null != innerObject.getString("num") && !"".equals(innerObject.getString("num"))) {
                        sportsWorkInfo.setCityTrainNum(Integer.parseInt(innerObject.getString("num")));
                    }
                }
                if ("4".equals(key)) {
                    sportsWorkInfo.setIzProvinceTrain(getYesOrNo(innerObject.getString("is")));
                    sportsWorkInfo.setProvinceTrainContent(innerObject.getString("content"));
                    if (null != innerObject.getString("num") && !"".equals(innerObject.getString("num"))) {
                        sportsWorkInfo.setProvinceTrainNum(Integer.parseInt(innerObject.getString("num")));
                    }
                }
                if ("5".equals(key)) {
                    sportsWorkInfo.setIzCountryTrain(getYesOrNo(innerObject.getString("is")));
                    sportsWorkInfo.setCountryTrainContent(innerObject.getString("content"));
                    if (null != innerObject.getString("num") && !"".equals(innerObject.getString("num"))) {
                        sportsWorkInfo.setCountryTrainNum(Integer.parseInt(innerObject.getString("num")));
                    }
                }
            }
        }

        //个人培训数据
        if (null != obj.get("exchange") && !"".equals(obj.get("exchange").toString())) {
            JSONObject exchangeObj = JSONObject.parseObject(obj.get("exchange").toString());
            for (String key : exchangeObj.keySet()) {
                JSONObject exchangeBean = exchangeObj.getJSONObject(key);
                if ("1".equals(key)) {
                    sportsWorkInfo.setIzSchoolDiscussion(getYesOrNo(exchangeBean.getString("is")));
                    sportsWorkInfo.setSchoolDiscussionContent(exchangeBean.getString("content"));
                    if (null != exchangeBean.getString("num") && !"".equals(exchangeBean.getString("num"))) {
                        sportsWorkInfo.setSchoolDiscussionNum(Integer.parseInt(exchangeBean.getString("num")));
                    }
                }
                if ("2".equals(key)) {
                    sportsWorkInfo.setIzAreaDiscussion(getYesOrNo(exchangeBean.getString("is")));
                    sportsWorkInfo.setAreaDiscussionContent(exchangeBean.getString("content"));
                    if (null != exchangeBean.getString("num") && !"".equals(exchangeBean.getString("num"))) {
                        sportsWorkInfo.setAreaDiscussionNum(Integer.parseInt(exchangeBean.getString("num")));
                    }
                }
                if ("3".equals(key)) {
                    sportsWorkInfo.setIzCityDiscussion(getYesOrNo(exchangeBean.getString("is")));
                    sportsWorkInfo.setCityDiscussionContent(exchangeBean.getString("content"));
                    if (null != exchangeBean.getString("num") && !"".equals(exchangeBean.getString("num"))) {
                        sportsWorkInfo.setCityDiscussionNum(Integer.parseInt(exchangeBean.getString("num")));
                    }
                }
                if ("4".equals(key)) {
                    sportsWorkInfo.setIzProvinceDiscussion(getYesOrNo(exchangeBean.getString("is")));
                    sportsWorkInfo.setProvinceDiscussionContent(exchangeBean.getString("content"));
                    if (null != exchangeBean.getString("num") && !"".equals(exchangeBean.getString("num"))) {
                        sportsWorkInfo.setProvinceDiscussionNum(Integer.parseInt(exchangeBean.getString("num")));
                    }
                }
                if ("5".equals(key)) {
                    sportsWorkInfo.setIzCountryDiscussion(getYesOrNo(exchangeBean.getString("is")));
                    sportsWorkInfo.setCountryDiscussionContent(exchangeBean.getString("content"));
                    if (null != exchangeBean.getString("num") && !"".equals(exchangeBean.getString("num"))) {
                        sportsWorkInfo.setCountryDiscussionNum(Double.parseDouble(exchangeBean.getString("num")));
                    }
                }
            }
        }

        //个人教学科研情况
        if (null != obj.get("topic") && !"".equals(obj.get("topic").toString())) {
            JSONObject topicObj = JSONObject.parseObject(obj.get("topic").toString());
            for (String key : topicObj.keySet()) {
                JSONObject topicBean = topicObj.getJSONObject(key);
                sportsWorkInfo.setIzSchoolSportResearchTopic(getYesOrNo(topicBean.getString("is")));
                sportsWorkInfo.setSchoolSportResearchContent(topicBean.getString("content"));
            }
        }

        //工作感知
        if (null != obj.get("feel") && !"".equals(obj.get("feel").toString())) {
            List<String> certificate = (List<String>) obj.get("feel");
            sportsWorkInfo.setWorkStrengthSelfEvalution(sysDictMapper.getDictItemByValue("work_strength_self_evalution", certificate.get(1)));
            sportsWorkInfo.setWorkEnvironmentEvalution(sysDictMapper.getDictItemByValue("evalution_type", certificate.get(2)));
            sportsWorkInfo.setWorkMotivationEvalution(sysDictMapper.getDictItemByValue("evalution_type", certificate.get(3)));
            sportsWorkInfo.setWorkPositivitySelfEvalution(sysDictMapper.getDictItemByValue("evalution_type", certificate.get(4)));

        }
        //说说您跟体育的渊源或者有趣的故事
        if(null != obj.get("story") && !"".equals(obj.get("story").toString())) {
            sportsWorkInfo.setSportStory(obj.get("story").toString());
        }
        sportsWorkInfoMapper.insert(sportsWorkInfo);
    }

    public String getYesOrNo(String isStr){
        if ("是".equals(isStr) || "有".equals(isStr)){
            return "0";
        }
        return "1";
    }

    /**
     * 学段中文转成数字  1-小学，2-初中，3-高中，4-其他
     * @param xueduan
     * @return
     */
    public Integer xueduanCode(String xueduan) {
        if (xueduan.equals("小学")){
            return 1;
        }
        if (xueduan.equals("初中")){
            return 2;
        }
        if (xueduan.equals("高中")){
            return 3;
        }
        return 4;
    }

    public void parsefile(String fileStr, String fileType, String projectId){

    }

    public static void main(String[] args) {
        String url = "http://www.huolixiaoyuan.com/upload/20240330/c44ae4d72ee93ffaa92a8765f6e3dda9.jpg";
        System.out.println(url.indexOf("/"));
        System.out.println(url.substring(url.lastIndexOf("/")+1 , url.lastIndexOf(".")));
        System.out.println(url.replace("http://www.huolixiaoyuan.com/upload","https://nike-huolixiaoyuan.oss-rg-china-mainland.aliyuncs.com/rz_uploads"));
    }
}